<template>
  <div>
    <div class="shuxing-box" style="display: block; height: 50px">
      <div style="width: 100%; font-size: 16px; color: #1046ad">
        测试厂商 (直送) :test
      </div>
      <div style="float: left; color: #1046ad; width: 50%">
        <div
          id="countDiv"
          style="width: 100%; margin-bottom: 2px; font-size: 12px"
        >
          测试厂商(直送)共有4个货品未分组，已设置2个分组含0个货品
        </div>
      </div>
      <div
        style="
          width: 40%;
          margin-right: 50px;
          float: right;
          color: #1046ad;
          font-size: 16px;
        "
      >
        已选择货品
      </div>
    </div>

    <div class="flex_x_start">
      <div class="left">
        <div class="left_content">
          <el-form ref="searchObj1" :model="searchObj1" :inline="true">
            <el-form-item
              :label="$t('ims.supplier.brandCode')"
              :prop="elPropModel.brandCode"
            >
              <el-input
                v-model="searchObj1.brandCode"
                :placeholder="$t('common.enter') + $t('ims.supplier.brandCode')"
              />
            </el-form-item>
            <el-form-item
              :label="$t('ims.supplier.merchandiseCode')"
              :prop="elPropModel.merchandiseCode"
            >
              <el-input
                v-model="searchObj1.merchandiseCode"
                :placeholder="
                  $t('common.enter') + $t('ims.supplier.merchandiseCode')
                "
              />
            </el-form-item>

            <el-form-item
              :label="$t('ims.supplier.merchandiseName')"
              :prop="elPropModel.merchandiseName"
            >
              <el-input
                v-model="searchObj1.merchandiseName"
                :placeholder="
                  $t('common.enter') + $t('ims.supplier.merchandiseName')
                "
              />
            </el-form-item>
            <el-form-item
              :label="$t('ims.supplier.merchandiseTypeCode')"
              :prop="elPropModel.merchandiseTypeCode"
            >
              <el-input
                v-model="searchObj1.merchandiseTypeCodes"
                :placeholder="
                  $t('common.enter') + $t('ims.supplier.merchandiseTypeCode')
                "
                @focus="openMerchandiseTypeCodeDailog1(this)"
              />
            </el-form-item>

            <el-form-item
              :label="$t('ims.supplier.merchandiseGroupCode')"
              :prop="elPropModel.merchandiseGroupCode"
            >
              <el-input
                v-model="searchObj1.merchandiseGroupCode"
                :placeholder="
                  $t('common.enter') + $t('ims.supplier.merchandiseGroupCode')
                "
              />
            </el-form-item>

            <el-button
              type="primary"
              @click="queryList1()"
            >{{ $t("common.query") }}
            </el-button>
          </el-form>
        </div>
        <DataTable
          ref="roleTable"
          :table-option="tableOption1"
          :table-data="selectTableData1"
          :column="column1"
          :pagination.sync="pagination1"
          @search-event="queryList1"
        />

        <material-type
          ref="materialType"
          @resetSearchObj="resetSearchObjHandle"
        />
      </div>

      <div class="flex_button">
        <br>
        <br>
        <br>
        <br>
        <el-button
          type="primary"
          @click="rightMove()"
        >{{ $t("common.rightArrow") }}
        </el-button>
        <br>
        <br>
        <el-button
          type="primary"
          @click="rightAllMove()"
        >{{ $t("common.rightDoubleArrow") }}
        </el-button>
        <br>
        <br>
        <el-button
          type="primary"
          @click="leftMove()"
        >{{ $t("common.leftArrow") }}
        </el-button>
        <br>
        <br>
        <el-button
          type="primary"
          @click="leftAllMove()"
        >{{ $t("common.leftDoubleArrow") }}
        </el-button>
      </div>

      <div class="flex">
        <div class="left_content">
          <el-form ref="searchObj2" :model="searchObj2" :inline="true">
            <el-form-item
              :label="$t('ims.supplier.brandCode')"
              :prop="elPropModel.brandCode"
            >
              <el-input
                v-model="searchObj2.brandCode"
                :placeholder="$t('common.enter') + $t('ims.supplier.brandCode')"
              />
            </el-form-item>
            <el-form-item
              :label="$t('ims.supplier.merchandiseCode')"
              :prop="elPropModel.merchandiseCode"
            >
              <el-input
                v-model="searchObj2.merchandiseCode"
                :placeholder="
                  $t('common.enter') + $t('ims.supplier.merchandiseCode')
                "
              />
            </el-form-item>

            <el-form-item
              :label="$t('ims.supplier.merchandiseName')"
              :prop="elPropModel.merchandiseName"
            >
              <el-input
                v-model="searchObj2.merchandiseName"
                :placeholder="
                  $t('common.enter') + $t('ims.supplier.merchandiseName')
                "
              />
            </el-form-item>
            <el-form-item
              :label="$t('ims.supplier.merchandiseTypeCode')"
              :prop="elPropModel.merchandiseTypeCode"
            >
              <el-input
                v-model="searchObj2.merchandiseTypeCode"
                :placeholder="
                  $t('common.enter') + $t('ims.supplier.merchandiseTypeCode')
                "
              />
            </el-form-item>

            <el-form-item
              :label="$t('ims.supplier.merchandiseGroupCode')"
              :prop="elPropModel.merchandiseGroupCode"
            >
              <el-input
                v-model="searchObj2.merchandiseGroupCode"
                :placeholder="
                  $t('common.enter') + $t('ims.supplier.merchandiseGroupCode')
                "
              />
            </el-form-item>

            <el-button
              type="primary"
              @click="queryList2()"
            >{{ $t("common.query") }}
            </el-button>
          </el-form>
        </div>
        <DataTable
          ref="roleTable"
          :table-option="tableOption2"
          :table-data="selectTableData2"
          :column="column2"
          :pagination.sync="pagination2"
          @search-event="queryList2"
        />
      </div>
    </div>

  </div>
</template>

<script>
import DataTable from '@/components/DataTable'
import imsApi from '@/api/ims/ims'
import MaterialType from './materialTypes'

export default {
  name: '',
  components: {
    DataTable,
    MaterialType
  },
  data() {
    return {
      elPropModel: {
        brandCode: 'brandCode',
        merchandiseCode: 'merchandiseCode',
        merchandiseName: 'merchandiseName',
        merchandiseTypeCode: 'merchandiseTypeCode',
        merchandiseGroupCode: 'merchandiseGroupCode'
      },
      searchObj1: {
        groupId: '2e100233-89c8-433f-9c3d-0ee430a45447',
        brandCode: null,
        merchandiseCode: null,
        merchandiseGroupCode: null,
        merchandiseTypeCodes: null,
        merchandiseName: null
      },
      // 分页
      pagination1: {
        pageNum: 1,
        pageSize: 10,
        total: 0
      },
      searchObj2: {
        groupId: '2e100233-89c8-433f-9c3d-0ee430a45447',
        merchandiseCode: null,
        brandCode: null,
        merchandiseGroupCode: null,
        merchandiseTypeCodes: null,
        merchandiseName: null
      },
      // 分页
      pagination2: {
        pageNum: 1,
        pageSize: 10,
        total: 0
      },
      selectTableData1: [],
      selectTableData2: [],
      tableOption1: {
        option: {
          enableSelected: true,
          height: 450,
          showHeaderBtn: false
        }
      },
      tableOption2: {
        option: {
          enableSelected: true,
          height: 450,
          showHeaderBtn: false
        }
      },
      column1: {
        // 表头
        data: [
          {
            label: this.$t('ims.supplier.merchandiseCode'),
            prop: 'merchandiseCode',
            show: true
          },
          {
            label: this.$t('ims.supplier.merchandiseName'),
            prop: 'merchandiseName',
            show: true
          }
        ]
      },
      column2: {
        // 表头
        data: [
          {
            label: this.$t('ims.supplier.merchandiseCode'),
            prop: 'merchandiseCode',
            show: true
          },
          {
            label: this.$t('ims.supplier.merchandiseName'),
            prop: 'merchandiseName',
            show: true
          }
        ]
      },
      moveRightData: [],
      moveLeftData: []
    }
  },
  watch: {},
  mounted() {
    this.queryList1()
    this.queryList2()
  },
  created() {
    this.$nextTick(() => {
      this.queryList1()
      this.queryList2()
    })
  },

  methods: {
    resetSearchObjHandle(select) {
      const menuCodes = []
      select.map(item => {
        menuCodes.push(item.merchandiseTypeCode)
      })
      this.searchObj1.merchandiseTypeCodes = menuCodes
      this.queryList1()
    },

    openMerchandiseTypeCodeDailog1(data) {
      imsApi.materialTypes().then(res => {
        /* this.$refs.authMenu.checkedKeys=res[0].data.map(v=>{
        return v.menuCode
      });*/
        this.$refs.materialType.treeData = res.data
        this.$nextTick(() => {
          this.$refs.materialType.role = data
          this.$refs.materialType.show()
        })
      })
    },
    queryList1() {
      const param = Object.assign(this.searchObj1, this.pagination1)
      imsApi.materialList(param).then((res) => {
        console.log('res = ' + res)
        this.selectTableData1 = res.data.list
        this.pagination1 = res.data.pagination
      })
    },

    queryList2() {
      const param = Object.assign(this.searchObj2, this.pagination2)
      imsApi.materialSelectedList(param).then((res) => {
        console.log('res = ' + res)
        this.selectTableData2 = res.data.list
        this.pagination2 = res.data.pagination
      })
    },
    rightMove() {
      // eslint-disable-next-line eqeqeq
      if (this.moveRightData.length == 0) {
        return
      }
      const list = []
      this.moveRightData.forEach((material) => {
        list.push(material.guid)
      })
      const param = {
        groupId: '2e100233-89c8-433f-9c3d-0ee430a45447',
        materialCodes: list
      }
      imsApi.materialSelect(param).then((res) => {
        this.$message.success(this.$t('common.addSuccess'))
        this.queryList1()
        this.queryList2()
      })
    },

    rightAllMove() {
      const param = { groupId: '2e100233-89c8-433f-9c3d-0ee430a45447' }
      imsApi.materialSelectAll(param).then((res) => {
        this.$message.success(this.$t('common.addSuccess'))
        this.queryList1()
        this.queryList2()
      })
    },
    leftMove() {
      // eslint-disable-next-line eqeqeq
      if (this.moveLeftData.length == 0) {
        return
      }
      const list = []
      this.moveLeftData.forEach((material) => {
        list.push(material.guid)
      })
      const param = {
        groupId: '2e100233-89c8-433f-9c3d-0ee430a45447',
        materialCodes: list
      }
      imsApi.materialUnSelect(param).then((res) => {
        this.$message.success(this.$t('common.addSuccess'))
        this.queryList1()
        this.queryList2()
      })
    },
    leftAllMove() {
      const param = { groupId: '2e100233-89c8-433f-9c3d-0ee430a45447' }
      imsApi.materialUnselectAll(param).then((res) => {
        this.$message.success(this.$t('common.addSuccess'))
        this.queryList1()
        this.queryList2()
      })
    },
    // tree勾选
    handleCheckChange() {
      // 获取所有已选menuId(包括半选状态)
      this.checkedNodes = this.$refs.authTree
        .getCheckedNodes()
        .concat(this.$refs.authTree.getHalfCheckedNodes())
      // 获取已选id
      this.checkedKeys = this.$refs.authTree.getCheckedKeys(true)
      // 获取全选状态
      const checkedNodesTable = this.$refs.authTree.getCheckedNodes()
      const table = this.dealCheckedNodes(checkedNodesTable, [])
      this.selectTableData = table
    },
    // 获取tree最小层级
    dealCheckedNodes(data, list) {
      data.map((item) => {
        if (item.portalMenuInfoVOList && item.portalMenuInfoVOList.length > 0) {
          this.dealCheckedNodes(item.portalMenuInfoVOList, list)
        } else {
          const nameList = this.findParent(item.menuCode)
          item.nameList = nameList
          list.push(item)
        }
      })
      return Array.from(new Set(list))
    },

    // 获取某个值的所有上级
    findParent(key, flattenTree = this.checkedNodes) {
      const parentArr = [] // 存储所有的父级元素
      function find(key, flattenTree) {
        flattenTree.forEach((ele) => {
          if (ele.menuCode === key) {
            parentArr.unshift(ele.menuName)
            find(ele.parentMenuCode, flattenTree)
          }
        })
      }

      find(key, flattenTree)
      return parentArr
    },
    show() {
      this.selectTableData = []
    },
    sizeChange1(data) {
      this.pagination1.pageNum = 1
      this.pagination1.pageSize = data
      this.queryList1()
    },
    currentChange1(data) {
      this.pagination1.pageNum = data
      this.queryList1()
    },
    selectionChange1(data) {
      this.moveRightData = data
    },
    sizeChange2(data) {
      this.pagination2.pageNum = 1
      this.pagination2.pageSize = data
      this.queryList2()
    },
    currentChange2(data) {
      this.pagination2.pageNum = data
      this.queryList2()
    },
    selectionChange2(data) {
      this.moveLeftData = data
    }
  }
}
</script>

<style lang="scss" scoped>
::v-deep .dialog {
  min-height: 800px;
}

::v-deep .el-dialog__wrapper .el-dialog__body {
  flex: 1 1 0%;
}
.flex_x_start {
  display: flex;
  align-items: flex-start;
  justify-content: flex-start;
}
.left {
  width: 40%;
  margin: 40px 5px 5px 5px;
  border: 1px solid #ebeef5;
}
.left_title {
  height: 50px;
  line-height: 50px;
  background-color: #f5f5f5;
  color: #000000;
  font-weight: 500;
  font-size: 14px;
  padding: 0 20px;
}
.left_content {
  padding: 10px;
  overflow-y: auto;
}

.flex_button {
  width: 100px;
  margin: 40px 5px 5px 5px;
  text-align: center;
}

.flex {
  flex: 1;
  width: 45%;
  margin: 40px 5px 5px 5px;
  border: 1px solid #ebeef5;
}
.text-show {
  display: flex;
  justify-content: space-around;
  align-items: center;
  padding: 10px 20px 10px 0;
  font-size: 16px;
  font-weight: 600;
}
</style>
